<?php
//基础控制器
namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use App\Models\ShopClerk;
use App\Models\UserCheck;
use App\Models\Shop;

class BaseController extends Controller
{
    //检验传入token，及设备号
    protected function CheckThisUser($token='',$device_id=''){
        //验证令牌
        $checkCache  = Cache::get($token);
        if (!$checkCache){
            return false;
        }
        if($checkCache->device_id!=$device_id){
            return false;
        }
        if($checkCache->role==-1){
             $checkShopUser = UserCheck::find($checkCache->id);
             if(!$checkShopUser){
                 return false;
             }
        }else{
            $checkShopUser = ShopClerk::find($checkCache->id);
            if(!$checkShopUser){
                return false;
            }
        }
        //返回用户
        return $checkCache;
    }


    //检验传入token，及设备号,返回商家的userid
    protected function getShopUserID($token='',$device_id=''){
        //验证令牌
        $checkCache  = Cache::get($token);
        if (!$checkCache){
            return false;
        }
        if($checkCache->device_id!=$device_id){
            return false;
        }

        if($checkCache->role==-1){  // 商家
            $checkShopUser = UserCheck::find($checkCache->id);
            if(!$checkShopUser){
                return false;
            }
            $checkCache->money = $checkShopUser->money;
        }else{          // 普通服务员
            $checkShopUser = ShopClerk::find($checkCache->id);
            if(!$checkShopUser){
                return false;
            }
            $checkCache->role = $checkShopUser->role;
        }
        //获取商户的用户ID
        if($checkCache->role !=-1){
            $shop = Shop::find($checkCache->shop_id);
            if(!$shop){
                return false;
            }
            $checkCache->shop_user_id = $shop->user_id;
        }else{
            $checkCache->shop_user_id = $checkCache->id;
        }
        return $checkCache;
    }





}
